<?php

namespace App\Http\Controllers\Order;

use App\Model\Order\MealSupplementBalance;
use Exception;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;
use App\Utils\TeacherRechargeUnit;

class MealSupplementBalanceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        try {
            $input = $request->all();
            if (empty($input['group'])) {
                $meal_supplement_balance = MealSupplementBalance::with(['users' => function ($query) {
                    $query->select('id', 'username', 'first_name')->with(['groups' => function ($query) {
                        $query->select('group_id', 'name');
                    }]);
                }])->whereHas('users', function ($query) use ($input) {
                    $query->where('username', 'like', '%' . $input['username'] . '%');
                })->paginate($input['page_size']);
            } else {
                $meal_supplement_balance = MealSupplementBalance::with(['users' => function ($query) use($input){
                    $query->select('id', 'username', 'first_name')
                        ->with(['groups' => function ($query) {
                            $query->select('group_id', 'name');
                        }]);
                }])->whereHas('users', function ($query) use ($input) {
                    $query->where('username', 'like', '%' . $input['username'] . '%')->whereHas('groups', function ($query) use ($input) {
                        $query->where('group_id', $input['group']);
                    });
                })->paginate($input['page_size']);
            }
            return $this->success(200, $meal_supplement_balance);
        } catch (Exception $e) {
            return $this->fail(500, ['Message ' => $e->getMessage()]);
        }
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(Request $request, $id)
    {
        try {
            $input = $request->all();
            $input["order_price"] = abs($input["order_price"]);
            if (Auth::user()->is_superuser && $input["isRecharge"] == 1) {
                $meal = TeacherRechargeUnit::recharge($input["order_price"], $id);
            } else {
                $meal = TeacherRechargeUnit::deduction($input["order_price"], $id);
            }
            return $this->success($meal['code'], $meal['data']);
        } catch (Exception $e) {
            return $this->fail(500, ['Message ' => $e->getMessage()]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try {
            return $this->success(200, ['MealSupplementBalanceController']);
        } catch (Exception $e) {
            return $this->fail(500, ['Message ' => $e->getMessage()]);
        }
    }
}
